package com.kudian.restful.controller;

import com.kudian.common.persistence.ResultObj;
import com.kudian.common.utils.DateUtils;
import com.kudian.common.utils.StringUtils;
import com.kudian.restful.entity.LoginSession;
import com.kudian.restful.entity.Message;
import com.kudian.restful.service.LoginSessionService;
import com.kudian.restful.service.MessageService;
import com.kudian.restful.service.UserBonusService;
import com.kudian.restful.vo.message.*;
import com.kudian.restful.vo.userbonus.QueryBonusByClient;
import com.kudian.restful.vo.userbonus.QueryBonusRetVO;
import com.kudian.restful.vo.userbonus.ReceiveVO;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2016/7/6.
 */
@RestController
@RequestMapping("message")
public class MessageController {
    private static final Logger logger = Logger.getLogger(MessageController.class);

    @Autowired
    private MessageService messageService;

    @Autowired
    private LoginSessionService loginSessionService;

    @RequestMapping(value = "sendSysMsg", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "发送系统消息", httpMethod = "POST", notes = "管理平台发送系统消息", response = ResultObj.class)
    public ResultObj sendSysMsg(@RequestBody SysMsgVO vo) {
        ResultObj o = new ResultObj();

        try {
            Message m = new Message();
            m.setSenderId(0);
            m.setReceiverId(0);
            m.setSentTime(DateUtils.getSysTimestamp());
            m.setReadTime(null);
            m.setReaded(false);
            m.setDeleted(false);
            m.setTitle(vo.getTitle());
            m.setMsg(vo.getMsg());
            messageService.save(m);
            o.setErrcode(0);
            o.setErrmsg("成功发送系统消息");
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("发送系统消息失败!");
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "sentmsg", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "发送点对点消息", httpMethod = "POST", notes = "发送点对点消息, receiverId=0时是直接给平台发送消息", response = ResultObj.class)
    public ResultObj sentmsg(@RequestBody SentMsgVO vo) {
        ResultObj o = new ResultObj();
        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("发送点对点消息失败!access_token为空");
            return o;
        }

        try {
            LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
            Message m = new Message();
            m.setSenderId(ent.getUserId());
            m.setReceiverId(vo.getReceiverId());
            m.setSentTime(DateUtils.getSysTimestamp());
            m.setReadTime(null);
            m.setReaded(false);
            m.setDeleted(false);
            m.setTitle(vo.getTitle());
            m.setMsg(vo.getMsg());
            messageService.save(m);
            o.setErrcode(0);
            o.setErrmsg("成功发送点对点消息");
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("发送点对点消息失败!");
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "queryMsgsByself", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询自己接收的消息", httpMethod = "POST", notes = "查询自己接收的消息", response = ResultObj.class)
    public QueryMsgRetVO queryMsgsByself(@RequestBody QueryMsgVO vo) {
        QueryMsgRetVO o = new QueryMsgRetVO();
        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("发送点对点消息失败!access_token为空");
            return o;
        }
        try {
            LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
            if (ent == null) {
                o.setErrcode(9999);
                o.setErrmsg("access_token已失效!");
                return o;
            }

            o.setItems(messageService.queryMsgsByself(vo));
            o.setErrcode(0);
            o.setErrmsg("成功查询自己接收的消息");
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("查询自己接收的消息失败!");
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "details", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "单条消息明细", httpMethod = "POST", notes = "单条消息明细", response = ResultObj.class)
    public MsgDetailRetVO details(@RequestBody DetailMsgVO vo) {
        MsgDetailRetVO o = new MsgDetailRetVO();
        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("单条消息明细失败!access_token为空");
            return o;
        }
        try {
            LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
            if (ent == null) {
                o.setErrcode(9999);
                o.setErrmsg("access_token已失效!");
                return o;
            }

            o.setInfo(messageService.details(vo));
            o.setErrcode(0);
            o.setErrmsg("成功取得单条消息明细");
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("取得单条消息明细失败!");
            logger.error(e);
        }
        return o;
    }

//    @RequestMapping(value = "read", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
//    @ApiOperation(value = "读消息", httpMethod = "POST", notes = "读消息", response = ResultObj.class)
//    public ResultObj read(@RequestBody ReadMsgVO vo) {
//        MsgDetailRetVO o = new MsgDetailRetVO();
//
//        return o;
//    }

    @RequestMapping(value = "delete", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除消息", httpMethod = "POST", notes = "删除消息", response = ResultObj.class)
    public ResultObj delete(@RequestBody DeleteMsgVO vo) {
        ResultObj o = new ResultObj();
        try {
            LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
            if (ent == null) {
                o.setErrcode(9999);
                o.setErrmsg("access_token已失效!");
                return o;
            }

            messageService.delete(vo);
            o.setErrcode(0);
            o.setErrmsg("成功删除消息");
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("删除消息失败!");
            logger.error(e);
        }
        return o;
    }
}
